package thread;

/**
 * Created by IntelliJ IDEA.
 * User: Sabbath
 * Date: 2009-12-28
 * Time: 21:47:33
 * To change this template use File | Settings | File Templates.
 */
public class ThreadTest1 {
    class ThreadB implements Runnable {
        public void run() {
            while (true) {
                try {
                    print("B");
                } catch (Exception e) {
                }
            }
        }
    }

    class ThreadE implements Runnable {
        public void run() {
            while (true) {
                try {
                    print("E");
                } catch (Exception e) {
                }
            }
        }
    }

    private synchronized void print(String s) {
        try {
            if (s.equals("B")) {
                this.notify();
                System.out.print("B");
                this.wait();
            } else if (s.equals("E")) {
                this.notify();
                System.out.print("E");
                this.wait();
            }
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        ThreadTest1 test = new ThreadTest1();
        new Thread(test.new ThreadB()).start();
        new Thread(test.new ThreadE()).start();
    }
}
